package com.atguigu.day02;

//线程间定制化调用通信

//*  * 多线程之间按顺序调用，实现A->B->C
//        *  * 三个线程启动，要求如下：
//        *  *
//        *  * AA打印5次，BB打印10次，CC打印15次
//        *  * 接着
//        *  * AA打印5次，BB打印10次，CC打印15次
//        *  * ......来10轮



import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Resource2{
    //假设A=1 B=2 C=3
    int info = 1;
    Lock lock  = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3= lock.newCondition();
    //打印5次
    public void print5() throws InterruptedException {
        lock.lock();
        try {
            //判断
            if(info!=1){
                //不符合条件，就等待
                condition1.await();
            }
            //干活
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName()+"干活"+i);
            }
            //通知
            info=2;
            //唤醒B进程（定制化）
            condition2.signal();
        }finally {
            lock.unlock();
        }

    }
    //打印10次
    public void print10() throws InterruptedException {
        lock.lock();
        try {
            //判断
            if(info!=2){
                //不符合条件，就等待
                condition2.await();
            }
            //干活
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName()+"干活"+i);
            }
            //通知
            info=3;
            //唤醒B进程（定制化）
            condition3.signal();
        }finally {
            lock.unlock();
        }

    }
    //打印5次
    public void print15() throws InterruptedException {
        lock.lock();
        try {
            //判断
            if(info!=3){
                //不符合条件，就等待
                condition3.await();
            }
            //干活
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName()+"干活"+i);
            }
            //通知
            info=1;
            //唤醒B进程（定制化）
            condition1.signal();
        }finally {
            lock.unlock();
        }

    }
}
public class ThreadOrderAccess {

    public static void main(String[] args) {
        Resource2 resource2 = new Resource2();
         new Thread(() -> {
             for (int i = 0; i < 10; i++) {
                 try {
                     resource2.print5();
                 } catch (InterruptedException e) {
                     throw new RuntimeException(e);
                 }
             }
                 }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    resource2.print10();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    resource2.print15();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "C").start();
    }
}
